const request = require("../../utils/request")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        talkList: [],
        content: "",
        queryParam: {
            currPage: 1,
            pageSize: 10,
        },
        total: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getTalkList()
    },
    inputChange(e) {
        this.setData({
            content: e.detail.value
        })
    },
    getTalkList() {
        let that = this
        request({
            url: "/suggests",
            data: that.data.queryParam,
            success: (res) => {
                console.log('获取成功', res);
                that.setData({
                    talkList: res.data.records,
                    total: res.data.total
                })
            },
            fail: (err) => {
                console.log('获取失败', err);
            }
        })
    },
    lower() {
        if (this.data.talkList.length < this.data.total) {
            this.setData({
                "queryParam.currPage": this.data.queryParam.currPage + 1
            })
            this.getPushList()
        }
    },
    getPushList() {
        let that = this
        request({
            url: '/suggests',
            data: that.data.queryParam,
            success(data) {
                const list = that.data.talkList
                data.data.records.forEach(item => {
                    list.push(item)
                });
                that.setData({
                    talkList: list
                })
            },
            fail(err) {
                console.log('请求失败：', err);
            }
        })
    },
    submit(e) {
        // console.log('content', this.data.content);
        const token = wx.getStorageSync('token')
        if (!token) return wx.showToast({
          icon: "none",
          title: '请先登录',
      })
        const mobile = JSON.parse(wx.getStorageSync('userinfo')).mobile
        if (!mobile) return wx.showToast({
            icon: "none",
            title: '请登录完善信息后再进行发布',
        })
        if (this.data.content == '') return wx.showToast({
          icon: "none",
          title: '请输入内容后再进行发布',
      })
        let that = this
        request({
            url: "/suggests",
            method: "post",
            data: {
                userId: id,
                content: that.data.content
            },
            success: (res) => {
                console.log("发布成功", res);
                that.setData({
                    content: ""
                })
                that.getTalkList()
                wx.showToast({
                    title: '发布成功',
                })
            },
            fail: (err) => {
                console.log("发布失败", err);
                wx.showToast({
                    icon: "error",
                    title: '发布失败,请联系管理员',
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})